﻿using MDT.LifeSewagePlatform.Conmmon;
using MDT.LifeSewagePlatform.Entity;
using MDT.LifeSewagePlatform.Entity.View;
using MDT.LifeSewagePlatform.IRepository;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace MDT.LifeSewagePlatform.Repository
{
    /**
    * 命名空间: MDT.LifeSewagePlatform.Repository
    *
    * 功 能： N/A
    * 类 名： IpAddressRepository
    *
    * Ver 变更日期 负责人 变更内容
    * ───────────────────────────────────
    * V0.01 2022/2/14 15:33:49 
    *
    *┌──────────────────────────────────┐
    *│　***************************************************．　│
    *│　**********************　　　　　　　　　　　　　　│
    *└──────────────────────────────────┘
*/
    public class IpAddressRepository: HandleBaseRepository<IpAddressEntity>,IIpAddressRepository
    {
        public IpAddressRepository()
        {

        }

        public async Task<bool> AddAsync(IpAddressEntity ipAddress)
        {
            await Create(ipAddress,0);
            return await SaveChangesAsync();
           
        }
        
        public IQueryable<IpAddressView> GetIpAddresses(Expression<Func<IpAddressEntity, bool>> expression)
        {
            var query = from ip in _dbContext.IpAddress.AsNoTracking() select ip;
            return query.Where(expression).Select(ip=>new IpAddressView(){
            CreationTime=ip.CreationTime,
            DeleterId=ip.DeleterId,
            DeletionTime=ip.DeletionTime,
            IpAddress=ip.IpAddress,
            IsBad=ip.IsBad,
            IsDelete=ip.IsDelete,
            LastModificationTime=ip.LastModificationTime,
            LastModifierId=ip.LastModifierId,
            VisitCountPreMinute=ip.VisitCountPreMinute,
            IpView=ip.IpAddress.IntToIp(),
            Description=ip.Description,
            VisitCount=ip.VisitCount
            });
        }


        public IQueryable<IpAddressDto> GetIpAddressDto(Expression<Func<IpAddressEntity, bool>> expression)
        {
            var query = from ip in _dbContext.IpAddress.AsNoTracking() select ip;
            return query.Where(expression).Select(ip => new IpAddressDto()
            {
                IpAddress = ip.IpAddress,
                IsBad = ip.IsBad,
            });
        }
    }
}
